package com.alks.function.controller.finance.mtlcosts;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.RoleCheck;
import com.alks.common.aop.WebLogger;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.common.utils.excelUtils.nestExcel.NestExcelUtil;
import com.alks.entity.data.entity.PcApAmtFk;
import com.alks.entity.data.page.PageParam;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.request.finance.ryfinance.ApAmtHeadRequest;
import com.alks.function.data.vo.finance.ryfinance.ApAmtAggrVO;
import com.alks.function.data.vo.finance.ryfinance.ApAmtDetailVO;
import com.alks.function.service.finance.mtlcosts.IAllApAmtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author MS
 * @since 2024/7/12下午6:31
 */
@RestController
@ApiRestController("/mtlCosts/allApAmt")
@Api(value = "财务部", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
@RoleCheck(prgIds = "180213")
@Slf4j
public class AllApAmtController {
    @Autowired
    private IAllApAmtService service;

    @WebLogger
    @PostMapping("/tempQuery")
    @ApiOperation(value = "结算资料查询", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public ResponseInfo tempQuery(@RequestBody ApAmtHeadRequest request) {
        return service.tempQuery(request);
    }

    @WebLogger
    @PostMapping("/tempQuery/export")
    @ApiOperation(value = "结算资料查询导出", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public void tempQueryExport(@RequestBody ApAmtHeadRequest request, HttpServletResponse response) {
        service.tempQueryExport(request, response);
    }

    @WebLogger
    @PutMapping("/tempAdd")
    @ApiOperation(value = "结算", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public ResponseInfo tempAdd(@RequestParam @ApiParam(required = true, value = "结算月份") String financeYymm,
                                @RequestParam @ApiParam(required = true, value = "结算方式") String accountName,
                                @RequestParam @ApiParam(required = true, value = "厂商") String vendorName) {
        return service.tempAdd(financeYymm,accountName, vendorName);
    }

    @WebLogger
    @GetMapping("/back/vendor")
    @ApiOperation(value = "结算厂商-回显", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public ResponseInfo vendor(@RequestParam@ApiParam(required = true) String accountName,
                               @RequestParam@ApiParam(required = true) String financeYymm) {
        List<Map<String,String>> vendorList = service.vendor(financeYymm,accountName);
        return ResponseInfo.ok(vendorList);
    }

    @WebLogger
    @PostMapping("/tempDel")
    @ApiOperation(value = "取消结算", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public ResponseInfo tempDel(@RequestParam @ApiParam(required = true, value = "单据号码") String slipNo) {
        return service.tempDel(slipNo);
    }

    @WebLogger
    @PostMapping("/detailQuery")
    @ApiOperation(value = "结算明细查询", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public ResponseInfo detailQuery(@RequestBody PageParam request,
                                    @RequestParam @ApiParam(required = true, value = "单据号码") String slipNo,
                                    @RequestParam(required = false) @ApiParam(value = "是否合并（1：合并 其他则不合并）") Integer flag) {
        return service.detailQuery(request, slipNo,flag);
    }

    @WebLogger
    @PostMapping("/detailQuery/export")
    @ApiOperation(value = "结算明细查询导出", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public void detailQueryExport(HttpServletResponse response, @RequestParam @ApiParam(required = true, value = "单据号码") String slipNo) {
        List<String> heads = new ArrayList<>();
        heads.add("结算明细");
        new NestExcelUtil<ApAmtDetailVO>().exportData(service.detailQueryExport(slipNo, false), Collections.singletonList(heads), "结算明细", response);
    }

    @WebLogger
    @PostMapping("/detailQuery/aggrExport")
    @ApiOperation(value = "结算明细查询导出汇总", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public void detailQueryAggrExport(HttpServletResponse response, @RequestParam @ApiParam(required = true, value = "单据号码") String slipNo) {
        List<String> heads = new ArrayList<>();
        heads.add("结算明细汇总");
        List<ApAmtAggrVO> vos = service.detailQueryExport(slipNo, true).stream().map(e->{
            ApAmtAggrVO vo = new ApAmtAggrVO();
            BeanUtil.copyProperties(e, vo);
            return vo;
        }).collect(Collectors.toList());
        new NestExcelUtil<ApAmtAggrVO>().exportData(vos, Collections.singletonList(heads), "结算明细汇总", response);
    }

    @WebLogger
    @PutMapping("/cfm")
    @ApiOperation(value = "结算明细检核", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public ResponseInfo cfm(@RequestBody @ApiParam(required = true, value = "单据号码") List<String> slipNos,
                            @RequestParam(required = false) @ApiParam(value = "Y.检核 null.取消检核") String cfmFlag) {
        return service.cfm(slipNos, cfmFlag);
    }

    @WebLogger
    @PostMapping("/fkQuery")
    @ApiOperation(value = "付款记录查询", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public ResponseInfo fkQuery(@RequestParam @ApiParam(required = true, value = "单据号码") String slipNo) {
        return service.fkQuery(slipNo);
    }

    @WebLogger
    @PutMapping("/fkUp")
    @ApiOperation(value = "付款记录修改", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public ResponseInfo fkUp(@RequestBody PcApAmtFk request,
                             @RequestParam @ApiParam(required = true, value = "旧金额") BigDecimal amtOld,
                             @RequestParam @ApiParam(required = true, value = "旧厂商") String vendorNameOld) {
        return service.fkUp(request,amtOld,vendorNameOld);
    }

    @WebLogger
    @PostMapping("/fkAdd")
    @ApiOperation(value = "付款记录新增", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public ResponseInfo fkAdd(@RequestBody PcApAmtFk request) {
        return service.fkAdd(request);
    }

    @WebLogger
    @PostMapping("/fkDel")
    @ApiOperation(value = "付款记录删除", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public ResponseInfo fkDel(@RequestParam @ApiParam(required = true, value = "id") String id) {
        return service.fkDel(id);
    }

    @WebLogger
    @PutMapping("/aftPayProcess")
    @ApiOperation(value = "尾款处理", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public ResponseInfo aftPayProcess(@RequestBody @ApiParam("订单号") List<String> slipNos) {
        return service.aftPayProcess(slipNos);
    }

    @WebLogger
    @GetMapping("/detail/print")
    @ApiOperation(value = "明细打印", tags = "财务部-材料费用-结算应付作业(1+2+3+4)")
    public ResponseInfo detailPrint(@RequestParam @ApiParam("订单号") String slipNo) {
        return ResponseInfo.ok(service.detailPrint(slipNo));

    }


}